package com.lanou.renrenxin.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.data.BlackListContactsBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/8.
 */
public class BlackListActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {
    private ListView listView;
    private BlacklistAdapater adapter;
    private List<BlackListContactsBean> blackName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blacklist);

        listView = (ListView) findViewById(R.id.lv_blacklist);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getBlackList();//从网络获取黑名单,添加到blackName中
//        listView.setEmptyView(findViewById(R.id.tv_empty));//设置listview没有数据的时候有提示内容
        adapter = new BlacklistAdapater(blackName,this);
        listView.setAdapter(adapter);
    }

    private void getBlackList() {
        blackName = new ArrayList<>();
        List<String> username;
        username = EMContactManager.getInstance().getBlackListUsernames();
        for (int i = 0; i < username.size(); i++) {
            blackName.add(new BlackListContactsBean(username.get(i)));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.EIV_avatar:
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        removeOutBlacklist(position);
        return false;
    }
    /**
     * 移出黑名单
     *
     * @param
     */
   public void removeOutBlacklist(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("移出黑名单");
//        builder.setIcon(R.drawable.weixin);
        builder.setMessage("您确定要将此好友移出黑名单吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    EMContactManager.getInstance().deleteUserFromBlackList(blackName.get(position).getName_blackList());
                    adapter.removeData(position);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    /**
     * adapter
     */
    private class BlacklistAdapater extends BaseAdapter{
        private List<BlackListContactsBean>list = null;
        private Context context;

        public BlacklistAdapater(List<BlackListContactsBean> list, Context context) {
            this.list = list;
            this.context = context;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (null == convertView) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_blacklist, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.textView_item_head_apply = (TextView) convertView.findViewById(R.id.name);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView_item_head_apply.setText(list.get(position).getName_blackList());

            return convertView;
        }

        private class ViewHolder {
            private TextView textView_item_head_apply;
        }
        public void upData(List<BlackListContactsBean> list) {
            this.list = list;
            notifyDataSetChanged();
        }
        /**
         * 移除指定位置的数据
         *
         * @param position
         */
        public void removeData(int position) {
            this.list.remove(position);
            notifyDataSetChanged();
        }
    }

    /**
     * 返回
     *
     * @param view
     */
    public void back(View view) {
        finish();
    }
}
